package com.detection.controller;

import com.alibaba.fastjson2.JSONObject;
import com.detection.common.config.DetectionConfig;
import com.detection.common.constant.Constants;
import com.detection.common.perimeter.hikvision.AlarmLevelType;
import com.detection.common.perimeter.hikvision.DeviceStatus;
import com.detection.common.perimeter.hikvision.EventStatus;
import com.detection.hikvision.cache.CameraInfoCache;
import com.detection.hikvision.domain.EventVo;
import com.detection.hikvision.domain.HaikangReceiveDataVo;
import com.detection.common.domain.YqCameraEvent;
import com.detection.common.domain.YqCameraInfo;
import com.detection.tcp.client.NettyTcpClient;
import com.detection.tcp.domain.TCPMessage;
import com.detection.tcp.utils.ParseUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Objects;

import static com.detection.tcp.utils.DockingUtils.postForTCP;

/**
 * @author LH
 * @date 2022/10/21 14:39
 */
@Api(tags = "视频事件处理")
@RestController
@Slf4j
@RequestMapping("scp/cameraEvent")
public class HikvisionController {

    @Autowired
    private NettyTcpClient nettyTcpClient;

//    @Resource
//    private YqCameraEventService yqCameraEventService;
//    @Autowired
//    private ArtemisEventService artemisEventService;
//    @Autowired
//    private IYqCameraEventService iYqCameraEventService;
//    @Autowired
//    private IYqParkService iYqParkService;

    /**
     * 周界越界、区域入侵、人员徘徊、人员聚集、火点检测、监控点离线事件
     *
     * @return
     */
    @RequestMapping("receiveCameraEvent")
    public JSONObject receiveCameraEvent(@RequestBody HaikangReceiveDataVo vo) {
        System.out.println("区域入侵事件");
        System.out.println(vo);
        try {
            if (null != vo && !vo.getParams().getEvents().isEmpty()) {
                for (EventVo event : vo.getParams().getEvents()) {
                    //当事件不存在时保存到数据库
                    YqCameraEvent cameraEvent = event.toCameraEvent(new YqCameraEvent());
//                    if (null == iYqCameraEventService.selectByEventId(cameraEvent.getEventId())) {
                        YqCameraInfo yqCameraInfo = CameraInfoCache.get(cameraEvent.getSrcIndex());//从缓存中取出监控点信息
                        cameraEvent.setParkRegionIndex(null != yqCameraInfo ? yqCameraInfo.getParkRegionIndex() : null);//将所属园区写入监控事件信息中
                        cameraEvent.setEventStatus(EventStatus.PENDING.getStatus());
                        if (null != yqCameraInfo) {
//                            YqPark park = iYqParkService.selectByHKIndexCode(cameraEvent.getParkRegionIndex());
                            cameraEvent.setParkCode(yqCameraInfo.getCampCode());
                        }
                        cameraEvent.setIgnoreFlag(Constants.GLOBAL_NO);
                        //TODO 暂时将烟火和火点报警设置为高级其余为低级
                        if (Objects.equals(cameraEvent.getEventType(), 192515) || Objects.equals(cameraEvent.getEventType(), 192514)) {
                            cameraEvent.setEventLvl(AlarmLevelType.HIGH.getLevel());
                        } else {
                            cameraEvent.setEventLvl(AlarmLevelType.LOW.getLevel());
                        }
                        TCPMessage message = new TCPMessage();
                        message.setFlag("2");
                        message.setData(cameraEvent);
                        message.setCampCode(DetectionConfig.getCampCode());
                        nettyTcpClient.sendMessage(ParseUtils.parse(message));
                        postForTCP(message);
//                        iYqCameraEventService.insert(cameraEvent);
//                    }
                    if (Objects.equals(cameraEvent.getEventType(), 889196545)) {
                        YqCameraInfo info = CameraInfoCache.get(cameraEvent.getSrcIndex());

                        if (null != info) {
                            if (cameraEvent.getStatus() == 2) {//修改缓存中摄像头状态
                                info.setStatus(DeviceStatus.ONLINE.getStatus());
                            } else {
                                info.setStatus(DeviceStatus.OFFLINE.getStatus());
                            }
                            CameraInfoCache.updateStatus(info.getIndexCode(), info.getStatus());
                        }
                    }
                }
            }
            JSONObject object = new JSONObject();
            object.put("code", 0);
            object.put("msg", "success");
            return object;
        } catch (JsonProcessingException | ParseException e) {
            JSONObject object = new JSONObject();
            object.put("code", 0);
            object.put("msg", "");
            throw new RuntimeException(e);
        }
    }

}
